<?php
namespace common\service;
use common\models\Balance;
use common\models\TransferLog;
use common\models\TradeTransfer;
use common\models\Member;

/*
	转帐操作
*/

class TransferService
{

	public static function transfer($params, $member)
	{

		try{
			$transaction = \Yii::$app->db->beginTransaction();

			## 配置信息
			$conf = TradeTransfer::getOne(['from_btype' => $params['from_type'], 'to_btype' => $params['to_type']]);

			if(!$conf) return ['state' => false,'msg'=>'配置信息有误'];

			## 条件判断
			$res = self::condition($conf, $params, $member);
			if(!$res['state']) throw new \Exception($res['msg']);

			## 接收人
			$toMember = Member::getOne(['or','member_id='.$params['to_member_id'],"member_mobile='".$params['to_member_id']."'"],['member_id', 'member_name']);
			if(!$toMember) throw new \Exception("接收人不存在");

			## 创建日志
			if(!$logId = self::addLog($params, $conf, $member, $toMember)) throw new \Exception('日志生成失败');
            ## 操作账本 增加
            $res1 = Balance::addBalance($toMember['member_id'], $conf['to_btype'], $params['money'], 12, $logId, '转入');

            if(!$res['state']) throw new \Exception($res['msg']);
			## 操作账本 减少
			$res = Balance::reduceBalance($member['member_id'], $conf['from_btype'], $params['money'], 11, $logId, '转出');
			if(!$res['state']) throw new \Exception($res['msg']);

			if($conf['next_action']>0) echo '后续操作，暂省';

			$transaction->commit();
			return ['state' => true,'msg' => 'success'];
		}catch(\Exception $e)
		{
			$transaction->rollback();
			return ['state' => false,'msg' => $e->getMessage()]; 
		}		
	} 


	## 条件
	private static function condition($conf, $params,$member)
	{
		if(!$conf['state']) return ['state' => false , 'msg' => '暂不能此操作'];
		if($params['money'] <=0 ) return ['state' => false,'msg' => '金额不能小于0'];
		if($conf['min_num'] > 0 && $conf['min_num'] > $params['money'] ) return ['state' => false, 'msg' => '金额不能小于'.$conf['min_num']];
		if($conf['max_num'] > 0 && $conf['max_num'] < $params['money'] ) return ['state' => false, 'msg' => '金额不能大于'.$conf['max_num']];
		if($conf['num_times'] > 0 && $params['money']%$conf['num_times']) return ['state' => false, 'msg' => '金额要为'.$conf['num_times'].'的倍数'];

		$bInfo = Balance::getBalance($member['member_id'], $params['from_type']);

		if($bInfo['balance'] < $params['money'] ) return ['state' => false , 'msg' => '余额不足'];

		return ['state' => true, 'msg' => 'success'];
	}	

	private static function addLog($params, $conf, $member, $toMember)
	{
		$data = array();
		$data['from_member_id'] = $member['member_id'];
		$data['from_member_name'] = $member['member_name'];
		$data['to_member_id'] = $toMember['member_id'];
		$data['to_member_name'] = $toMember['member_name'];		
		$data['money'] = $params['money'];
		$data['from_type'] = $params['from_type'];
		$data['to_type'] = $params['to_type'];
		$data['fee'] = round($params['money'] * $conf['rate']/100, 2);	
		$data['state'] = 1;
		$data['create_time'] = time();

		$model = new TransferLog;
		if(!$model->load($data,'') || !$model->save()){
			return false;
		}
		return \Yii::$app->db->getLastInsertId();
	}


}